package com.gable.exhibition.controller;

import com.alibaba.fastjson.JSON;
import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.SupplierService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.SupplierView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Lesson on 2018/3/16.
 */
@Controller
@RequestMapping(value="/supplier")
public class SupplierController {

        Logger logger = Logger.getLogger(this.getClass());

        @Autowired
        SupplierService supplierService;

        @RequestMapping(value="/page")
        public String page(HttpServletRequest request, HttpServletResponse response){
            return "supplier/supplier-page";
        }

        @RequestMapping(value="/querySupplierList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil querySupplierList(HttpServletRequest request, HttpServletResponse response,SupplierView supplierView){
            try{
                ReturnResultUtil rru = supplierService.querySupplierList(supplierView);
                logger.info(JSON.toJSONString(rru));
                return rru;
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        }

        @RequestMapping(value="/addOrUpdateSupplier",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil addOrUpdateSupplier(HttpServletRequest request, HttpServletResponse response,SupplierView supplierView){
            try{
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Long userId = null;
                if(!StringUtil.isEmpty(request.getParameter("userId"))){
                    userId = Long.parseLong(request.getParameter("userId"));
                }
                User user = CacheData.getSessionUser(userId,1);
                if(supplierView.getSupplierId() == null){
                    supplierView.setCreateTime(df.format(new Date()));
                    supplierView.setCreatePerson(user.getUserId());
                }else{
                    supplierView.setUpdateTime(df.format(new Date()));
                    supplierView.setUpdatePerson(user.getUserId());
                }
                supplierView.setIsManufacturer(false);
                return supplierService.addOrUpdateSupplier(supplierView);
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500, MessageUtil.SAVEEXCEPTION);
        }

        @RequestMapping(value="/querySupplierInfo",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil querySupplierInfo(HttpServletRequest request, HttpServletResponse response){
            try{
                if(StringUtil.isEmpty(request.getParameter("supplierId"))){
                    return new ReturnResultUtil(500,MessageUtil.SUPPLIERIDISNULL);
                }
                Long supplierId = Long.parseLong(request.getParameter("supplierId"));
                ReturnResultUtil rru = supplierService.querySupplierInfo(supplierId);
                logger.info(JSON.toJSONString(rru));
                return rru;
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }

        @RequestMapping(value="/querySupplierListByManuId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil querySupplierListByManuId(HttpServletRequest request, HttpServletResponse response){
            try{
                if(StringUtil.isEmpty(request.getParameter("supplierId"))){
                    return new ReturnResultUtil(500,MessageUtil.SUPPLIERIDISNULL);
                }
                Long supplierId = Long.parseLong(request.getParameter("supplierId"));
                ReturnResultUtil rru = supplierService.querySupplierListByManuId(supplierId);
                logger.info(JSON.toJSONString(rru));
                return rru;
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }
}
